
import { getEvents } from '../../api/store.js';
import{getBanners} from '../../api/api.js'

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    productList:[],
    parameter: {
      'navbar': '1',
      'return': '1',
      'title': '最新EVENT',
    },
    navH: "",

    where: {
      keyword: '',
      page: 1,
      limit: 10,
    },
    nows:false,
    loadend:false,
    loading:false,
    loadTitle:'加载更多',
    banners:[],
    userInfo:{}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ 
      navH: app.globalData.navHeight,
    });
    this.get_product_list();
    this.getBanners()
  },
  getBanners(){
    let that = this
    getBanners().then(res=>{
      that.setData({
        banners:res.data.event_banner
      })
    })
  },

    /**
     * 商品详情跳转
     */
    goDetail: function (e) {
        let item = e.currentTarget.dataset.items;
        wx.navigateTo({ url: `/pages/event_detail/index?id=${item.product_id}&event_id=${item.id}&acId=${item.id}&public_date=${item.public_date}&state=${item.state}` });
    },

  searchSubmit: function (e) {
    var that = this;
    this.setData({ ['where.keyword']: e.detail.value, loadend: false, ['where.page']: 1 })
    this.get_product_list(true);
  },

  //查找产品
  get_product_list: function (isPage) {
    let that = this;
    if (that.data.loadend) return;
    if (that.data.loading) return;
    if (isPage === true) that.setData({ productList: [] });
    that.setData({ loading: true, loadTitle: '' });
    getEvents(that.data.where).then(res=>{
      let list = res.data.data;
      let productList = app.SplitArray(list, that.data.productList);
      let loadend = list.length < that.data.where.limit;
      that.setData({
        loadend: loadend,
        loading: false,
        loadTitle: loadend ? '已全部加载' : '加载更多',
        productList: productList,
        ['where.page']: that.data.where.page + 1,
      });
    }).catch(err=>{
      that.setData({ loading: false, loadTitle: '加载更多' });
    });
  },
  
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({['where.page']:1,loadend:false,productList:[]});
    this.get_product_list();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.get_product_list();
  },
})